"""
表格操作
"""
import xlrd
import xlutils.copy
from xlrd.book import Book
from conf import settings

new_workbook = None
table = None
sheet1 = None


def init_host_xlx():
    # 读取
    workbook = xlrd.open_workbook(settings.WORKBOOK, formatting_info=True)
    global table
    table = workbook.sheets()[0]

    # 复制写入表
    global new_workbook
    new_workbook = xlutils.copy.copy(workbook)
    global sheet1
    sheet1 = new_workbook.get_sheet(0)


def read():
    init_host_xlx()
    from db.models import Host
    host_list = []
    for i in range(1, table.nrows):
        host_list.append(Host(
            host_id=i,
            ip=table.cell_value(i, 0),
            account=table.cell_value(i, 1),
            role=table.cell_value(i, 2),
        ))
    return host_list


def write(host, row):
    pass


def excel_save(host, rowx):
    sheet1.write(rowx, 3, host.os_release)
    sheet1.write(rowx, 4, host.connect_status)
    sheet1.write(rowx, 5, host.sync_status)
    sheet1.write(rowx, 6, host.account_status)
    sheet1.write(rowx, 7, host.auth_status)
    sheet1.write(rowx, 8, host.is_bad)
    from core.src_thread import NEW_WORKBOOK
    new_workbook.save(NEW_WORKBOOK)


if __name__ == "__main__":
    # print(table)
    # print(table.nrows)
    # print(table.row(1))
    # print(type(table.row(1)[1]))
    print(read())
